#!/usr/bin/python
# -*- coding: UTF-8 -*-

import tkinter as tk
from tkinter import font, messagebox

from auto import Constant, Jenkins_auto, Gitlab_auto, Properties


# 获取屏幕大小
def get_screen_size():
    return window.winfo_screenwidth(), window.winfo_screenheight()


def do_j_task(task, project):
    # 打包
    if Constant.J_PKG_DEV == task:
        Jenkins_auto.start_package(Constant.BRANCH_DEV, project)
    elif Constant.J_PKG_RC == task:
        Jenkins_auto.start_package(Constant.BRANCH_RC, project)
    elif Constant.J_PKG_PRD == task:
        Jenkins_auto.start_package(Constant.BRANCH_PRD, project)
    # 发布
    elif Constant.J_DEP_DEV == task:
        Jenkins_auto.start_deploy_dev(project)
    elif Constant.J_DEP_RC == task:
        Jenkins_auto.start_deploy_rc(project)
    elif Constant.J_DEP_PRD == task:
        Jenkins_auto.start_deploy_prd(project)
    else:
        messagebox.showerror(title='Error', message='非法操作，请重试！')


def btn_j_click():
    task_name = radio_var1.get()
    project_name = str(pro_list.curselection())
    print('【Jenkins操作=%(s1)s】-【Project=%(s3)s】' % {"s1": task_name, "s3": project_name})
    if '()' == str(pro_list.curselection()):
        messagebox.showwarning('温馨提示', '请选择一个项目后再进行此操作！！')
    else:
        project_name = pro_list.get(pro_list.curselection())
        do_j_task(task_name, project_name)


def do_g_task(task, project):
    if Constant.RC_TAG == task:
        Gitlab_auto.start_tag(Properties.get_tag_rc(), project)
    elif Constant.PRD_TAG == task:
        Gitlab_auto.start_tag(Properties.get_tag_prd(), project)
    else:
        messagebox.showerror(title='Error', message='非法操作，请重试！')


def btn_g_click():
    task_name = radio_var2.get()
    project_name = str(pro_list.curselection())
    print('【GitLab操作=%(s2)s】-【Project=%(s3)s】' % {"s2": task_name, "s3": project_name})
    if '()' == str(pro_list.curselection()):
        messagebox.showwarning('温馨提示', '请选择一个项目后再进行此操作！！')
    else:
        project_name = pro_list.get(pro_list.curselection())
        do_g_task(task_name, project_name)


# ======  start 窗口布局  =========task_name
window = tk.Tk()
window.title('办公利器之超好用又神奇的自动化工具 - Beta.1.0')
x, y = 600, 460
scr_x, scr_y = get_screen_size()
window.geometry("%dx%d+%d+%d" % (x, y, (scr_x-x)/2, (scr_y-y)/2))
window.resizable(0, 0)

frm_1 = tk.Frame(window, width=600, height=230, bg='CadetBlue').pack()
frm_2 = tk.Frame(window, width=600, height=230, bg='DarkSeaGreen').pack()
frm_3 = tk.Frame(window, width=155, height=600, bg='Beige').place(x=275, y=0, anchor=tk.NW)

# =====  Title  ======
ft = font.Font(family='Courier New', size=14, weight=font.BOLD)
lb1 = tk.Label(frm_1, text='Jenkins打包发布', font=ft, bg='CadetBlue')
lb2 = tk.Label(frm_1, text='GitLab新增TAG', font=ft, bg='DarkSeaGreen')
lb3 = tk.Label(frm_1, text='项目名称', font=ft, bg='Beige')
lb1.place(x=5, y=5, anchor=tk.NW)
lb2.place(x=5, y=245, anchor=tk.NW)
lb3.place(x=280, y=5, anchor=tk.NW)

# =====  Jenkins  ======
radio_var1 = tk.StringVar()
radio_var1.set(Constant.J_PKG_DEV)

dev_package = \
    tk.Radiobutton(frm_1, text='打包-Dev', variable=radio_var1, value=Constant.J_PKG_DEV, width=10, bg='CadetBlue')
rc_package = \
    tk.Radiobutton(frm_1, text='打包-RC ', variable=radio_var1, value=Constant.J_PKG_RC, width=10, bg='CadetBlue')
prd_package = \
    tk.Radiobutton(frm_1, text='打包-PRD', variable=radio_var1, value=Constant.J_PKG_PRD, width=10, bg='CadetBlue')
dev_deploy = \
    tk.Radiobutton(frm_1, text='发布-Dev', variable=radio_var1, value=Constant.J_DEP_DEV, width=10, bg='CadetBlue')
rc_deploy = \
    tk.Radiobutton(frm_1, text='发布-RC ', variable=radio_var1, value=Constant.J_DEP_RC, width=10, bg='CadetBlue')
prd_deploy = \
    tk.Radiobutton(frm_1, text='发布-PRD', variable=radio_var1, value=Constant.J_DEP_PRD, width=10, bg='CadetBlue')

dev_package.place(x=10, y=50, anchor=tk.NW)
rc_package.place(x=10, y=85, anchor=tk.NW)
prd_package.place(x=10, y=120, anchor=tk.NW)
dev_deploy.place(x=100, y=50, anchor=tk.NW)
rc_deploy.place(x=100, y=85, anchor=tk.NW)
prd_deploy.place(x=100, y=120, anchor=tk.NW)

# =====  Gitlab  ======
radio_var2 = tk.StringVar()
radio_var2.set(Constant.RC_TAG)

rc_tag = \
    tk.Radiobutton(frm_2, text='Tag-RC ', variable=radio_var2, value=Constant.RC_TAG, width=10, bg='DarkSeaGreen')
prd_tag = \
    tk.Radiobutton(frm_2, text='Tag-PRD', variable=radio_var2, value=Constant.PRD_TAG, width=10, bg='DarkSeaGreen')

rc_tag.place(x=10, y=300, anchor=tk.NW)
prd_tag.place(x=10, y=335, anchor=tk.NW)

# =====  Project List  ======
pro_name = tk.StringVar()
pro_list = tk.Listbox(frm_3, listvariable=pro_name, bg='Beige', width=21, height=410, exportselection=False)
pro_list.place(x=275, y=50, anchor=tk.NW)
for item in Constant.PROJECT_LIST:
    pro_list.insert('end', item)
pro_list.grab_current()

# =====  Button  =======
btn_J = tk.Button(frm_1, width=10, height=1, text='开始执行', command=btn_j_click, bg='DarkSeaGreen')
btn_G = tk.Button(frm_2, width=10, height=1, text='开始执行', command=btn_g_click, bg='CadetBlue')
btn_J.place(x=500, y=120, anchor=tk.NW)
btn_G.place(x=500, y=320, anchor=tk.NW)

window.mainloop()
